/**
 *---------------------------------------------------------------------------------------------------------------------
 *  @copyright Copyright (c) 2022  <dx_65535@163.com>.
 *
 *  @file       : main_pipe_server.cc
 *  @author     : Xiang.D (dx_65535@163.com)
 *  @version    : 1.0
 *  @brief      : Blog: https://mp.weixin.qq.com/s/eoCPWMGbIcZyxvJ3dMjQXQ
 *  @date       : 2024/11/22
 *
 *
 *  Change History:
 *  <Date>     | <Version> | <Author>       | <Description>
 *---------------------------------------------------------------------------------------------------------------------
 *  2024/11/22 | 1.0.0.1   | Xiang.D        | Create file
 *---------------------------------------------------------------------------------------------------------------------
 *
 */
#include <list>
#include <memory>
#include <stdio.h>
#include <unistd.h>
#include "PPipe.h"
#include "EpollEventHandler.h"

using namespace std;

#define SPR_LOGD(fmt, args...) printf("PipeServer D: %4d " fmt, __LINE__, ##args)
#define SPR_LOGW(fmt, args...) printf("PipeServer W: %4d " fmt, __LINE__, ##args)
#define SPR_LOGE(fmt, args...) printf("PipeServer E: %4d " fmt, __LINE__, ##args)

int main(int argc, char *argv[])
{
    if (argc != 2) {
        SPR_LOGE("Usage: %s <name>\n", argv[0]);
        return -1;
    }

    string name = argv[1];
    auto epollHandler = EpollEventHandler::GetInstance();

    auto pPipeSrv = make_shared<PPipe>(name, [&](int fd, string bytes, void* arg) {
        auto pPipeSrv = reinterpret_cast<PPipe*>(arg);
        if (!pPipeSrv) {
            SPR_LOGE("pPipeSrv is nullptr\n");
            return;
        }

        SPR_LOGD("# RECV [%d]> %s\n", pPipeSrv->GetEvtFd(), bytes.c_str());
    });

    pPipeSrv->AddToPoll();
    epollHandler->EpollLoop();
    return 0;
}
